package com.itheima.web.response; /**
 * @author Xiao
 * @date 2021/8/30
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ResponseDemo03", value = "/resp03")
public class ResponseDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        response.setCharacterEncoding("UTF-8");
        //1. 获取字符输出流    getWriter()
        //      一次请求响应结束后，response对象就会被销毁掉，所以不要手动关闭流。
        PrintWriter writer = response.getWriter();
        //2. 写出数据   write()
        writer.write("aaa");
        //content type:告诉浏览器返回的数据类型是HTML类型数据，这样浏览器才会解析HTML标签，否则会当成字符串解析
        //response.setHeader("content-type", "text/html");
        //设置响应的数据格式及数据的编码
        response.setContentType("text/html;charset=utf-8");
        writer.write("<h1>标题h1</h1>");
        writer.write("你好");


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
